﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb

Public Class frmDangNhap

    Private flag As Boolean = False 'Dung kiem soat timer
    Public LoginLoaiND As String = ""
    Public LoginTenND As String = ""
    Public LoginPassND As String = ""
    Private connect As ConnectDB
    Public XinChao As String
    Dim dt As New DataTable

    Private Sub frmDangNhap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
    End Sub

#Region "Hàm kiêm tra tinh hop le cua ket noi"
    Public Function Logged(ByVal U As String, ByVal P As String) As Integer
        Try
            connect = New ConnectDB()
            Dim i As Integer
            dt = connect.GetData("select * from NHANVIEN where IDNV = '" + txtUser.Text + "'")
            For i = 0 To dt.Columns.Count() - 1
                dt.Rows(0)(i).ToString()
            Next
            connect.Close()
            If dt.Rows.Count() > 0 Then
                'So sanh password
                If P = CStr(dt.Rows(0)(1).ToString) Then
                    'Nếu username và password đều hợp le
                    ' Dang nhap thanh cong
                    Logged = 0
                    LoginLoaiND = dt.Rows(0)(7).ToString
                    LoginTenND = dt.Rows(0)(0).ToString
                    LoginPassND = dt.Rows(0)(1).ToString
                    XinChao = "Xin Chào! " + dt.Rows(0)(7).ToString + " " + dt.Rows(0)(2).ToString
                Else
                    'Sai pass và trả về giá trị -1
                    Logged = -1
                End If
            Else
                'Khong tim thay username trong CSDL, trả về -2
                Logged = -2
            End If
        Catch ex As Exception
            Logged = -2
        End Try

    End Function
#End Region

#Region "Xử lý các nút"

    Private Sub btnDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangNhap.Click
        If txtUser.Text = "" Then
            MsgBox("Vui lòng nhập tên đăng nhập!")
            txtUser.Focus()
            Exit Sub
        End If
        'Yeu cau nguoi su dung nhap lai pass
        If txtPassWord.Text = "" Then
            MsgBox("Vui lòng nhập mật khẩu để đăng nhập hệ thống!")
            txtPassWord.Focus()
            Exit Sub
        End If
        
        'Goi ham kiem tra username va pass
        Dim x As Integer = Logged(txtUser.Text, txtPassWord.Text)
        If x = -1 Then
            MsgBox("Bạn nhập sai password")
            txtPassWord.Text = ""
            txtPassWord.Focus()
        ElseIf x = -2 Then
            MsgBox("username không tồn tại")
            txtUser.Text = ""
            txtPassWord.Text = ""
            txtUser.Focus()
        Else
            flag = True
        End If
        If flag Then
            TimerClosing.Start()
            TimerClosing.Interval = 100
        End If
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Try
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("Lỗi đóng chương trình")
        End Try
    End Sub
#End Region

#Region "Event FormClosing da hoan tat"
    Private Sub FormLogin_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If flag Then
            e.Cancel() = True
            Me.TimerClosing.Enabled = True
        End If
    End Sub
#End Region

#Region "Xử lý đồng hồ"

    Private Sub TimerClosing_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerClosing.Tick
        Me.Opacity -= 0.05
        If Me.Opacity = 0 Then
            TimerClosing.Stop()
            Timer1.Stop()
            flag = False
            Me.Visible = False
            frmMain.Show()
            frmMain.WindowState = FormWindowState.Maximized
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblChaoMung.Visible = Not lblChaoMung.Visible
    End Sub
#End Region

End Class